'use strict';

const mongoose = require('mongoose'),
    orderModel = mongoose.model('Order');

module.exports = {
    addOrder: addOrder,
    updateOstateByOid:updateOstateByOid,
    findOrderByClient:findOrderByClient,
    findOrderByOid:findOrderByOid,
    findOrderByOstateandClient:findOrderByOstateandClient
}

function addOrder(order,callback){
    orderModel.create(order,function(error,data){
        callback(null,data);
    });
}

function findOrderByClient(cid, callback) {
    orderModel.find({'client._id':cid},
                function(error, data) {
                    callback(null, data);
                })
}

function findOrderByOid(oid, callback) {
    orderModel.findOne({'_id':oid},
                function(error, data) {
                    callback(null, data);
                })
}

function findOrderByOstateandClient(cid, ostate, callback) {
    orderModel.find({'client._id':cid,'ostate':ostate},
                function(error, data) {
                    console.log(data);
                    callback(null, data);
                })
}


function updateOstateByOid(oid, oldstate, callback) {
    orderModel.update({'_id':oid},
                     {$set:{'ostate':oldstate}},
                    function(error,data) {
                        orderModel.findOne({'_id':oid},function(error, data) {
                            console.log('db,update....')
                            console.log(data);
                            callback(null, data);
                        })
                        
                    });
    
}
